package com.moleq.mgpos.activities;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.moleq.adapter.FreeFuncAdapter;
import com.moleq.adapter.FunctionPanelAdapter;
import com.moleq.base.EditableListener;
import com.moleq.dialog.MessageDialog;
import com.moleq.dialog.SelectFuncDetailDialog;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.itemsetting.tools.MGCreateFiles;
import com.moleq.logic.MoleQConstants;
import com.moleq.models.FreeFunction;
import com.moleq.models.FunctionPanel;
import com.moleq.models.GlobalInfo;
import com.moleq.mgpos.activities.R;
import com.moleq.service.FreeFuncService;
import com.moleq.service.FunctionPanelService;
import com.moleq.service.ServiceManager;
import com.moleq.util.MoleQUtil;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.view.Window;
import android.widget.AbsoluteLayout;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class FunctionSettingActivity extends Activity {
	
	private Button btn_exit,btn_edit;
	private ImageButton ibtn_next;
	private ListView lv_func_list;
	private static ListView lv_func_panel_list;
	
	private FunctionPanelService mFunctionPanelService;
	private FreeFuncService mFreeFuncService;
	private List<FunctionPanel> listFunctionPanel = null;
	private List<FreeFunction> listFreeFunc = null;
	private FreeFuncAdapter freeFuncAdapter = null;
	private FunctionPanelAdapter functionPanelAdapter = null;
	private boolean result = false;
	
	FunctionPanel mFunctionPanel;
	String func_label,p1,func_name,func_label2 = "";
	int p2,func_key,func_no,func_order;
	double p3;
	int msgType = 0;
	
	private MessageDialog mMessageDialog;
	
	Date now = new Date();
	private MGCreateFiles mMGCreateFiles = new MGCreateFiles();
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_function_setting);
        
        findControl();
        ibtn_next.setOnClickListener(onClickListener);
        btn_exit.setOnClickListener(onClickListener);
        btn_edit.setOnClickListener(onClickListener);
        lv_func_list.setOnItemClickListener(onItemClickListener);
        lv_func_panel_list.setOnItemClickListener(onItemClickListener);
        mFunctionPanelService = ServiceManager.getInstance().mFunctionPanelService;
        mFreeFuncService = ServiceManager.getInstance().mFreeFuncService;
        
        bindFreeFuncData();
        bindFunctionPanelData();
	}
	
	private void findControl(){
		ibtn_next = (ImageButton) findViewById(R.id.ibtn_next);
		btn_edit = (Button) findViewById(R.id.btn_edit);
		btn_exit = (Button) findViewById(R.id.btn_exit);
		lv_func_list = (ListView) findViewById(R.id.lv_func_list);
		lv_func_panel_list = (ListView) findViewById(R.id.lv_func_panel_list);
	}
	
	private void bindFreeFuncData(){
		listFreeFunc = mFreeFuncService.getFreeFunctionForFuncPanel();
		if (listFreeFunc == null) {
			return;
		}
		if (freeFuncAdapter == null) {
			freeFuncAdapter = new FreeFuncAdapter(this, listFreeFunc);
		}
		lv_func_list.setAdapter(freeFuncAdapter);
	}
	private void bindFunctionPanelData(){
		insertDefaultData();
		listFunctionPanel = mFunctionPanelService.getAllFunctionPanel();
		if (listFunctionPanel == null) {
			return;
		}
		if (functionPanelAdapter == null) {
			functionPanelAdapter = new FunctionPanelAdapter(this, listFunctionPanel);
		}
		lv_func_panel_list.setAdapter(functionPanelAdapter);
	}
	
	private void insertDefaultData(){
		int count = mFunctionPanelService.getSignOutCount();
        if (count == 0) {
        	msgType = 1;
        	Message("There is no Sign Out function. Do you want to add it now?", mMessageDialog.MESSAGE_BOX_YES_NO,mMessageDialog.ICON_INFO);
		}
	}
	
	private OnClickListener onClickListener = new OnClickListener() {
		
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.ibtn_next:
				if (listFreeFunc.size() == 0) return;
				Keyboard();
				int index = freeFuncAdapter.GetSelectedIndex();
				func_no = mFunctionPanelService.getMaxPanelNo()+1;
				func_order = mFunctionPanelService.getMaxPanelOrder()+1;
				String appr_fl = "0";
				func_key = listFreeFunc.get(index).getFf_id();
				func_name = mFunctionPanelService.getFuncNameByFuncKey(func_key);
				func_label = listFreeFunc.get(index).getFf_name();
				p1 = "null";
				p2 = 0;
				if (func_key == MoleQConstants.FUNCTION_SHIFT_TAX) {
					p2 = 1;
				}
				p3 = 0;
				
				FunctionPanel fp = new FunctionPanel();
				fp.setTp_no(func_no);
				fp.setTp_order(func_order);
				fp.setTp_func_key(func_key);
				fp.setTp_label_file(func_name);
				fp.setTp_label_file2(func_label2);
				fp.setTp_appl_fl(appr_fl);
				fp.setTp_parameter1(p1);
				fp.setTp_parameter2(p2);
				fp.setTp_parameter3(p3);
				fp.setCreate_datetime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
				fp.setUpdate_datetime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
				switch (func_key) {
				case 0:
					result = mFunctionPanelService.insertFuncionPanel(fp);
					if (GlobalInfo.getInstance().isMGNetwork){
						mMGCreateFiles.createrFile(fp.toMGData("INSERT"));
					}
					ShowMessage(result, 3);
					listFunctionPanel = mFunctionPanelService.getAllFunctionPanel();
					functionPanelAdapter.setSource(listFunctionPanel);
					functionPanelAdapter.notifyDataSetChanged();
					if (listFunctionPanel.size() > 0) {
						functionPanelAdapter.ScrollToRow(func_order - 1);
						lv_func_panel_list.setSelection(func_order - 1);
					}else {
						functionPanelAdapter.ScrollToRow(0);
						lv_func_panel_list.setSelection(0);
					}
					break;
				default:
					SelectFuncDetailDialog mSelectFuncDetailDialog = new SelectFuncDetailDialog(FunctionSettingActivity.this, fp);
					mSelectFuncDetailDialog.show();
					break;
				}
				break;
			case R.id.btn_exit:
				int count = mFunctionPanelService.getSignOutCount();
				if (count == 0) {
					msgType = 2;
		        	Message("There is no Sign Out function. Do you want to add it now?", mMessageDialog.MESSAGE_BOX_YES_NO,mMessageDialog.ICON_INFO);
				}else{
					msgType = 0;
					Message(getResources().getString(R.string.msg_exit_program),
							mMessageDialog.MESSAGE_BOX_YES_NO,mMessageDialog.ICON_INFO);
				}
				break;
			case R.id.btn_edit:
				listFunctionPanel = functionPanelAdapter.getListFuncPanel();
				if (listFunctionPanel.size() <= 0) return;
				int selectIndex = functionPanelAdapter.GetSelectedIndex();
				if (selectIndex < 0) return;
				if (selectIndex >= listFunctionPanel.size()) return;
				mFunctionPanel = listFunctionPanel.get(selectIndex);
				func_key = mFunctionPanel.getTp_func_key();
				
				switch (func_key) {
				case 0:
					break;
				default:
					SelectFuncDetailDialog mSelectFuncDetailDialog = new SelectFuncDetailDialog(FunctionSettingActivity.this, mFunctionPanel);
					mSelectFuncDetailDialog.show();
					break;
				}
			}
		}
	};
	
	public void ModifyChangeAdapter(int order){
		listFunctionPanel = mFunctionPanelService.getAllFunctionPanel();
		functionPanelAdapter.setSource(listFunctionPanel);
		functionPanelAdapter.notifyDataSetChanged();
		if (listFunctionPanel.size() > 0) {
			functionPanelAdapter.ScrollToRow(order - 1);
			lv_func_panel_list.setSelection(order - 1);
		}else {
			functionPanelAdapter.ScrollToRow(0);
			lv_func_panel_list.setSelection(0);
		}
	}
		
	private OnItemClickListener onItemClickListener = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> parent, View view,
				int position, long id) {
			switch (parent.getId()) {
			case R.id.lv_func_list:
				freeFuncAdapter.ScrollToRow(position);
				break;
			case R.id.lv_func_panel_list:
				functionPanelAdapter.ScrollToRow(position);
				break;
			}
		}
	};
	
	public void ShowMessage(boolean re,int type){
		//type 1-insert or update 2-delete 3-show complete
		switch (type) {
		case 1:
			if (re == false) {
				Message("Save Error.",
						mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_INFO);
			}
			break;
		case 2:
			if (re == false) {
				Message("Delete Error.",
						mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_INFO);
			}
			break;
		case 3:
			if (re) {
				Toast.makeText(FunctionSettingActivity.this, "Save Completed.",
						Toast.LENGTH_SHORT).show();
			}else {
				Message("Save Error.", mMessageDialog.MESSAGE_BOX_OK,
						mMessageDialog.ICON_INFO);
			}
			break;
		}
	}
	
	private EditableListener mEditableListener = new EditableListener() {
		
		public void onEdited(String str) {
			if (str.equals(FunctionPanelAdapter.FUNCPANEL_EDITED)) {
				SetEnabledFalse(1);
			}
		}
	};
	
	public void SetEnabledFalse(int mode){
		switch (mode) {
		case 1:
			lv_func_list.setEnabled(false);
			ibtn_next.setEnabled(false);
			btn_exit.setEnabled(false);
			break;
		}
	}
	
	public static void set_lv_func_panel_list_Selection(int index){
		lv_func_panel_list.setSelection(index);
	}
	
	public void Keyboard(){
		InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		inputMethodManager.hideSoftInputFromWindow(FunctionSettingActivity.this
				.getCurrentFocus().getWindowToken(), 
				InputMethodManager.HIDE_NOT_ALWAYS);
	}
	
	private OnClickMessageListener onClickMessageListener = new OnClickMessageListener(){

		public void onClickYes() {
			// TODO Auto-generated method stub
			switch (msgType) {
			case 0:
				finish();
				break;
			case 1:
			case 2:
				FunctionPanel fp = new FunctionPanel();
				fp.setTp_no(mFunctionPanelService.getMaxPanelNo()+1);
				fp.setTp_order(mFunctionPanelService.getMaxPanelOrder()+1);
				fp.setTp_func_key(MoleQConstants.FUNCTION_SIGN_OUT);
				fp.setTp_label_file(mFunctionPanelService.getFuncNameByFuncKey(MoleQConstants.FUNCTION_SIGN_OUT));
				fp.setTp_label_file2(func_label2);
				fp.setTp_appl_fl("0");
				fp.setTp_parameter1(p1);
				fp.setTp_parameter2(p2);
				fp.setTp_parameter3(p3);
				fp.setCreate_datetime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
				fp.setUpdate_datetime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
				mFunctionPanelService.insertFuncionPanel(fp);
				if (GlobalInfo.getInstance().isMGNetwork){
					mMGCreateFiles.createrFile(fp.toMGData("INSERT"));
				}
				listFunctionPanel = mFunctionPanelService.getAllFunctionPanel();
				functionPanelAdapter.setSource(listFunctionPanel);
				functionPanelAdapter.notifyDataSetChanged();
				break;
			}
		}

		public void onClickNo() {
			// TODO Auto-generated method stub
			switch (msgType) {
			case 2:
				finish();
				break;
			default:
				mMessageDialog.dismiss();
				break;
			}
		}

		public void onClickOK() {
			// TODO Auto-generated method stub
			mMessageDialog.dismiss();
		}
		
	};
	public void Message(String text,int type,int icon){
		mMessageDialog = new MessageDialog(FunctionSettingActivity.this);
		mMessageDialog.setStyle(type);
		mMessageDialog.setMessageIcon(icon);
		mMessageDialog.setMessage(text);
		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
		mMessageDialog.show();
	}
}
